package no.hist.rtc.rtchat_old;

import no.hist.rtc.rtchat.R;
import android.app.Activity;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private final int portnummer = 1250;
	private final String TAGG = "CHAT";
	private Klient k;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_me);
        setIP(getMyIp());
        k = new Klient(portnummer);
    }
    
	@SuppressWarnings("deprecation")
	private String getMyIp(){
    	WifiManager wm = (WifiManager)getSystemService(WIFI_SERVICE);
    	WifiInfo wii = wm.getConnectionInfo();
    	return Formatter.formatIpAddress(wii.getIpAddress());
    }
    
    private void setIP(String ip){
    	((TextView)findViewById(R.id.dinIP)).setText("My IP: " + ip);
    }
    
    public void onClick(View v){
    	try{
    		Log.d(TAGG, "onClick");
    		Intent a = new Intent(this, ChatActivity.class);
    		Log.d(TAGG, "nytt Intent laget");
    		a.putExtra("portnr", portnummer);
    		a.putExtra("ip", ((EditText)findViewById(R.id.ip_tjener)).getText().toString());
    		startActivity(a);
    		Log.d(TAGG, "aktivitet startet!");
    	}
    	catch(Exception e){
    		Log.d(TAGG, e.toString());
    		Toast.makeText(getBaseContext(), R.string.kan_ikke_koble_opp, Toast.LENGTH_LONG).show();
    	}
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0,0,0,getString(R.string.avslutt));
        return true;
    }
    

	public boolean onOptionsItemSelected(MenuItem item){

    	switch(item.getItemId()){
	    	case 0:
	    		finish();
	    		return true;
    	}
		return false;
    }
}
